<?php
/**
 * 
 * +------------------------------------------------------------+
 * @category Registry 
 * +------------------------------------------------------------+
 * 通用存储类，帮助管理全局数据
 * +------------------------------------------------------------+
 *
 * @author anzai <236049524@qq.com>
 * @copyright http://www.judianyun.com 2012
 * @version 1.0
 *
 * Modified at : 2012-02-13 11:01:23
 *
 */
class Registry extends ArrayObject {
	///当前类的类名
	private static $_registryClassName = 'Registry';
	
	///Registry对象
	private static $_registry = null;
	
	//检索默认注册表实例
	public static function getInstance()
    {
        if (self::$_registry === null) {
            self::init();
        }

        return self::$_registry;
    }
	
    //默认注册表设置实例到一个指定的实例
	public static function setInstance(Registry $registry)
    {
    	try{
	        if (self::$_registry !== null) {
	            throw new E('Registry is already initialized');
	        }
	        self::setClassName(get_class($registry));
	        self::$_registry = $registry;
    	}catch(E $e){
    		$e->getMsg();
    	}
    }
    
    //设置类名使用默认注册表
 	public static function setClassName($registryClassName = 'Registry')
    {
        if (self::$_registry !== null) {
            throw new E('Registry is already initialized');
        }

        if (!is_string($registryClassName)) {
            throw new E("Argument is not a class name");
        }

        self::$_registryClassName = $registryClassName;
    }
    
    ///初始化默认注册表实例
	protected static function init()
    {
        self::setInstance(new self::$_registryClassName());
    }
    
    //取消设置的默认注册表实例
	public static function _unsetInstance()
    {
        self::$_registry = null;
    }
    
    //获取方法,类似方法offsetGet()
	public static function get($index)
    {
        $instance = self::getInstance();

        if (!$instance->offsetExists($index)) {
            return null;
        }

        return $instance->offsetGet($index);
    }
    
    //设置方法，同offsetSet()功能一致。
	public static function set($index, $value)
    {
        $instance = self::getInstance();
        $instance->offsetSet($index, $value);
    }
    
	public function offsetExists($index)
    {
        return array_key_exists($index, $this);
    }
    
	public function __get($index){
		if($this->offsetExists($index))
			return $this->offsetGet($index);
		else
			return '';
	}
}
?>